<?php namespace Common\Model;
use Hdphp\Model\Model;
class Goods extends Model{
	
	protected $table = "360mall_Goods";
	protected $validate = array(
	 array("g_name","required","请输入属性名称",3,3),
	  array("g_number","required","请输入商品货号",3,3),
	array("g_marketprice","num:0,1000000000000","价格必须为数字",3,3),  
	array("g_shopprice","num:0,1000000000000","价格必须为数字",3,3),  
	array("g_click","num:0,1000000000000","点击次数必须为数字",3,3),  
	);
	//自动完成
	protected $auto = array(
	  array("g_aid","aid","method",3,1),
	  array("g_time","time","function",3,1),
	);
	//自定义方法
	public function aid(){
		return $_SESSION['aid'];
	}
	
	//添加
	public function store(){
		$GoodAttrModel = new \Common\Model\Goodattr;
		$GdetailModel = new \Common\Model\Gdetail;
		//自动验证
		if(!$this->create()) return false;
//		if(!$GdetailModel->create()) {
//			$this->error = $GdetailModel->getError();
//			return false;
//		}
		//执行商品表的添加$gid返回商品的自增id
		$gid = $this->add();
		foreach ($_POST['attr'] as $k => $v) {
			$data = array(
			   "ga_gid"=>$gid,
			   "ga_taid"=>$k,
			   "ga_value"=>$v,
			);
			$GoodAttrModel->add($data);	
		}
       foreach ($_POST['spec'] as $k1 => $v1) {
		foreach ($v1['added'] as $k2 => $v2) {
			$data = array(
				 "ga_taid"=>$k1,
				 "ga_gid"=>$gid,
				 "ga_addprice"=>$v2,
				 "ga_value"=>$v1['color'][$k2],
				 );
				
			$GoodAttrModel->add($data); 	 
			}
			
    }
				//执行商品详细表的添加
				if(isset($_POST['photos'])){
					$small ="";
					$medium ="";
					$big = "";
					$path = "Upload/thumb/".date("Ymd")."/";
					if(is_dir($path)&&!isset($path)){
						mkdir($path,0777,true);
					}
				 foreach ($_POST['photos'] as $k4 => $v4) {
				 
				    $small.=Image::thumb($v4,$path."small/".basename($v4),200,200,5).","; 
				    $medium.=Image::thumb($v4,$path."medium/".basename($v4),800,800,5).","; 
				    $big.=Image::thumb($v4,$path."big/".basename($v4),1000,1107,5).","; 
				  }
                  $Gdetaildata = array(
                    "gd_small" =>$small,
                    "gd_medium"=>$medium,
                    "gd_big"=>$big,
                    "gd_gid"=>$gid,
                    "gd_info"=>$_POST['gd_info'],
                    "gd_service"=>$_POST['gd_service'],
                  );
				   $GdetailModel->add($Gdetaildata);
               }
			return true ;
		
	}
//编辑
		public function edit($gid){
			$GoodAttrModel = new \Common\Model\Goodattr;
		$GdetailModel = new \Common\Model\Gdetail;
			if(!$this->create()) return false;
			//执行商品表的修改
			 $this->save();
			//执行详情表的修改
			//xiansahnchu 
			$GoodAttrModel->where("ga_gid",$gid)->delete();
			if(isset($_POST['attr'])){
				foreach ($_POST['attr'] as $k => $v) {
					$data = array(
					   "ga_taid"=>$k,
					   "ga_value"=>$v,
					   "ga_gid"=>$gid,
					);
					$GoodAttrModel->add($data);	
				}
			}
			if(isset($_POST['spec'])){
			 foreach ($_POST['spec'] as $k1 => $v1) {
		foreach ($v1['added'] as $k2 => $v2) {
			$data = array(
				 "ga_taid"=>$k1,
				 "ga_gid"=>$gid,
				 "ga_addprice"=>$v2,
				 "ga_value"=>$v1['color'][$k2],
				 );
				
			$GoodAttrModel->add($data); 	 
			}
				}	
			}
      
	   //执行商品详细表的xiugai
				if(isset($_POST['photos'])){
					$small ="";
					$medium ="";
					$big = "";
					$path = "Upload/thumb/".date("Ymd")."/";
					if(is_dir($path)&&!isset($path)){
						mkdir($path,0777,true);
					}
				 foreach ($_POST['photos'] as $k4 => $v4) {
				 
				    $small.=Image::thumb($v4,$path."/small/".basename($v4),200,200,5).","; 
				    $medium.=Image::thumb($v4,$path."/medium/".basename($v4),400,400,5).","; 
				    $big.=Image::thumb($v4,$path."/big/".basename($v4),800,800,5).","; 
				  }
                  $Gdetaildata = array(
                    "gd_small" =>$small,
                    "gd_medium"=>$medium,
                    "gd_big"=>$big,
                    "gd_info"=>$_POST['gd_info'],
                    "gd_service"=>$_POST['gd_service'],
                  );
				   $GdetailModel->where("gd_gid",$gid)->update($Gdetaildata);
               }
	   
	   return true;
 }
			public function del($gid){
				//删除商品表
				$this->where("g_id",$gid)->delete();
				//删除详情表
				Db::table("360mall_gdetail")->where("gd_gid",$gid)->delete();
				//删除属性表
				Db::table("360mall_goods_attr")->where("ga_gid",$gid)->delete();
			}
}